<?php
namespace easemob;
class User extends IM
{   
    /**
     * 在环信上注册一个用户
     * @param unknown $username
     * @param unknown $passwrod
     * @param unknown $nickname
     * @return boolean|string   成功返回uuid，失败返回false
     */
    function create($username,$passwrod,$nickname){
        $url=$this->geturl('users');
        $data=[
            [
                'username'=> $username,
                'password'=> $passwrod,
                'nickname'=>$nickname,
            ]
        ];
        
        $ret=$this->sentPost($url, $data);
        if(isset($ret['entities'][0]['uuid'])){
            return $ret['entities'][0]['uuid'];
        }else{
            return false;
        }       
    }
    
    /**
     * 删除一个用户
     * @param unknown $username
     * @return boolean
     */
    function delete($username){
        $url=$this->geturl('users/'.$username);
        $ret=$this->sentDelete($url);
        if(isset($ret['entities'][0])){
            return true;
        }else{
            return false;
        }   
    }
    
    /**
     * 修改用户的呢称
     * @param unknown $username
     * @param unknown $newnickname
     * @return boolean
     */
    function changeNickName($username,$newnickname){
        $url=$this->geturl('users/'.$username);
        $data=['nickname'=>$newnickname];
        $ret=$this->sentPut($url,$data);
        if(isset($ret['entities'][0])){
            return true;
        }else{
            return false;
        }   
    }
    
    /**
     * 获取用户所在的群
     * @param unknown $username
     * @return boolean|mixed
     */
    function getJoinedGroups($username){
        
        $url=$this->geturl('users/'.$username.'/joined_chatgroups');
        $ret=$this->sentGet($url);
        return isset($ret['data'])?$ret['data']:false;
        return $ret;
    }
    
    /**
     * 将用户强制下线
     * @param unknown $username
     * @return boolean
     */
    function disConnect($username){
        
        $url=$this->geturl('users/'.$username.'/disconnect');
        $ret=$this->sentGet($url);
        if(isset($ret['data']['result'])){
            return $ret['data']['result'];
        }else{
            return false;
        }
    }
    
    /**
     * 获取用户状态，offline online
     * @param unknown $username
     * @return boolean|string
     */
    function getStatus($username){
        //GET /{org_name}/{app_name}/users/{username}/status
        $url=$this->geturl('users/'.$username.'/status');
        $ret=$this->sentGet($url);        
        if(isset($ret['data'][$username])){
            return $ret['data'][$username];
        }else{
            return false;
        }
        
    }
    
    function sendMsg($username,$to,$message){
        
    }
    
}